<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/23 0023
 * Time: 下午 14:53
 */
class myListHelper
{

    public  $data;
    private $pager;
    public $model;
    private $script = null;


    public function __construct($data,$pager,$model)
    {
        $this->data = $data;
        $this->pager = $pager;
        $this->model = $model;

    }

    public function createHeads()
    {
        $html = '<tr>';
        foreach($this->model->rules as $k=>$v)
        {
            $html .= '<th';
            if(isset($v['thStyle']) && empty($v['thStyle']))
            {
                $html .=' style="'.$v['thStyle'].'"';
            }
            $html .= '>'.$v['head'].'</th>';
        }
        if(!empty($this->model->ops) && isset($this->model->ops['head']) && !empty($this->model->ops['actions']))
        {
            $html.='<th';
            if(isset($this->model->ops['head']['thStyle']))
            {
                $html.=' style="'.$this->model->ops['head']['thStyle'].'"';
            }
            $html.='>'.$this->model->ops['head']['label'].'</th>';
        }
        $html.='</tr>';
        return $html;
    }

    public function hasValue($arr,$key)
    {
        return isset($arr[$key]) && !empty($arr[$key]);
    }

    public function createCol($rule,$value)
    {
        if(!$this->hasValue($rule,'type'))
        {
            $rule['type'] = 'text';
        }
        $html = '<td';
        if($this->hasValue($rule,'tdStyle'))
        {
            $html .= ' style="'.$rule['tdStyle'].'"';
        }
        $html .= '>';
        if($rule['type'] == 'img')
        {
            $html .= '<img src="'.$value.'"';
            if($this->hasValue($rule,'pic') && $this->hasValue($rule['pic'],'style'))
            {
                $html .= ' style="'.$rule['pic']['style'].'"';
            }
            $html .= '>';
        }
        else if($rule['type'] == 'radio')
        {
            foreach($rule['radio'] as $v)
            {
                if($v['value'] == $value)
                {
                    $html .= $v['label'];
                }
            }
        }
        else
        {
            $html .= $value;
        }

        $html .= '</td>';
        return $html;
    }

    public function createRow($item)
    {
        $html = '';
        foreach($this->model->rules as $k=>$v)
        {
            $html .=$this->createCol($v,isset($item[$k]) ? $item[$k] : '');
        }
        return $html;
    }

    public function createRows()
    {
        $html = '';
        foreach($this->data as $v)
        {
            $html .= '<tr>'.$this->createRow($v);
            $html .= $this->createOps($v);
            $html .= '</tr>';
        }
        return $html;
    }

    public function createPages()
    {
        $html = '';
        if($this->pager->getPageCount() <= 1)
        {
            return $html;
        }
        $html .= '<tr><td colspan="'.$this->model->getRulesCount().'"><div class="pagelist">';
        $html .= $this->pager->getPageList().'</div></td></tr>';
        return $html;
    }

    public function createTable()
    {
        $this->script = '';
        if(empty($this->model->rules))
        {
            return '';
        }
        $html = '<table class="table table-hover text-center">';
        if(!empty($this->data))
        {
            $html .= $this->createHeads();
            $html .= $this->createRows();
            $html .= $this->createPages();
            if(isset($this->model->ops['actions']))
            {
                if(isset($this->model->ops['actions']['del']))
                {
                    $this->script .= 'delConfirm("delItem","'.$this->model->ops['actions']['del']['link'].'");';
                }
            }

        }
        $html .= '</table>';
        return $html;
    }

    public function createScript()
    {
        if(empty($this->script))
        {
           return '';
        }
        return '<script>'.$this->script.'</script>';
    }

    public function createOps($item)
    {
        $html = '';
        if (!empty($this->model->ops) && isset($this->model->ops['head']) && !empty($this->model->ops['actions']))
        {
            $linkKey = $this->model->ops['head']['linkKey'];
            $html .= '<td';
            if (isset($this->model->ops['head']['tdStyle']))
            {
                $html .= ' style="' . $this->model->ops['head']['tdStyle'] . '"';
            }
            $html .= '><div class="button-group">';
            foreach($this->model->ops['actions'] as $k=>$v)
            {
                if($k == 'edit')
                {
                    $html .='<a class="button border-main" href="'.$v['link'].$item[$linkKey].'"><span class="icon-edit"></span> '.$v['label'].'</a>';
                }
                else if($k == 'del')
                {
                    $html .='<a class="button border-red delItem" data-id="'.$item[$linkKey].'"><span class="icon-trash-o"></span> '.$v['label'].'</a>';
                }
            }
            $html .= '</div></td>';
        }
        return $html;
    }

}
